#!/bin/bash

# generate /must-gather/version file
. version
echo "uccp/must-gather" > /must-gather/version
version >> /must-gather/version

# Named resource list, eg. ns/uccp-config
named_resources=()

# Resource groups list, eg. pods
group_resources=()

# Cluster Version Information
named_resources+=(ns/uccp-cluster-version)
group_resources+=(clusterversion)

# Operator Resources
group_resources+=(clusteroperators)

# Certificate Resources
group_resources+=(certificatesigningrequests)

# Machine/Node Resources
group_resources+=(nodes)

# Namespaces/Project Resources
named_resources+=(ns/default ns/uccp ns/kube-system ns/uccp-etcd)

# Storage Resources
group_resources+=(storageclasses persistentvolumes volumeattachments csidrivers csinodes volumesnapshotclasses volumesnapshotcontents)

# Image-source Resources
group_resources+=(imagecontentsourcepolicies.operator.uccp.io)

# Networking Resources
group_resources+=(networks.operator.uccp.io)

# NodeNetworkState
resources+=(nodenetworkstates nodenetworkconfigurationenactments nodenetworkconfigurationpolicies)

# Run the Collection of Resources using inspect
# running across all-namespaces for the few "Autoscaler" resources.
oc adm inspect --dest-dir must-gather --rotated-pod-logs "${named_resources[@]}"
group_resources_text=$(IFS=, ; echo "${group_resources[*]}")
oc adm inspect --dest-dir must-gather --rotated-pod-logs "${group_resources_text}"

# Gather Insights Operator Archives
/user/bin/gather_insights

# Gather monitoring data from the cluster
/usr/bin/gather_monitoring

# Gather optional operator resources from all namespaces
/usr/bin/gather_olm

# Gather API Priority and Fairness Endpoints
/usr/bin/gather_priority_and_fairness

# Gather etcd information
/usr/bin/gather_etcd

# Gather Validation and Mutating Admission Webhook Objects
# TODO the kube-apiserver operator should list these as related resources
/usr/bin/gather_admission_webhooks

# Gather Service Logs (using a supplemental Script); Scoped to Masters.
/usr/bin/gather_service_logs master

# Gather Windows Kubernetes component logs
/usr/bin/gather_windows_node_logs

# Gather HAProxy config files
/usr/bin/gather_haproxy_config

# Gather kas startup and termination logs
/usr/bin/gather_kas_startup_termination_logs

# Gather network logs
/usr/bin/gather_network_logs

# Gather metallb logs
/usr/bin/gather_metallb_logs

# force disk flush to ensure that all data gathered is accessible in the copy container
sync
